package at.guigu.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/user6")
public class UserControllerSix {
    // 单文件上传
    @ResponseBody
    @RequestMapping("/quick1")
    public void save1(@RequestParam("username") String username, @RequestParam("uploadFile") MultipartFile uploadFile) throws IOException {
        System.out.println(username);
        System.out.println(uploadFile);
        // 获取上传文件名
        String originalFilename = uploadFile.getOriginalFilename();
        // 保存文件
        uploadFile.transferTo(new File("F:\\node\\idea\\test\\" + originalFilename));
    }
    //多文件上传 方式一:文件name属性的属性名不一样
    @ResponseBody
    @RequestMapping("/quick2")
    public void save2(@RequestParam("username") String username, @RequestParam("uploadFile1") MultipartFile uploadFile1, @RequestParam("uploadFile2") MultipartFile uploadFile2) throws IOException {
        System.out.println(username);
        System.out.println(uploadFile1);
        System.out.println(uploadFile2);
        // 获取上传文件1的文件名
        String originalFilename1 = uploadFile1.getOriginalFilename();
        // 保存文件1
        uploadFile1.transferTo(new File("F:\\node\\idea\\test\\" + originalFilename1));
        // 获取上传文件2的文件名
        String originalFilename2 = uploadFile2.getOriginalFilename();
        // 保存文件2
        uploadFile2.transferTo(new File("F:\\node\\idea\\test\\" + originalFilename2));
    }
    // 多文件上传 方式二:文件name属性的属性名一样，此时用MultipartFile对象数组
    @ResponseBody
    @RequestMapping("/quick3")
    public void save3(@RequestParam("username") String username, @RequestParam("uploadFiles") MultipartFile[] uploadFiles) throws IOException {
        System.out.println(username);
        for (MultipartFile file : uploadFiles) {
            System.out.println(file);
            // 获取上传文件名
            String originalFilename = file.getOriginalFilename();
            // 保存文件
            file.transferTo(new File("F:\\node\\idea\\test\\" + originalFilename));
        }
    }
}
